/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>

#include "atom.h"
#include "debug.h"
#include "console.h"
#include <string.h>
#include "iosdrv.h"
#include "syslog.h"
#include "rtc.h"
#include "shell.h"
#include "numberical.h"

#if defined(COM_USE_CONSOLE) && defined(COM_USE_SHELL_CMD)

static uint8_t is_key_press(void)
{
	uint8_t c = 0xff;
	
    if (DebugPortIsReadable()) {
        c = DebugPortGetChar();
    }

    return c;
}

static enConsoleResult version(int argc, char** argv)
{
    DBG_COLOR_GREEN();
    KPrint(APP_NAME"\r\n""Ver."APP_VERSION" "__DATE__" "__TIME__"\r\n");
    DBG_COLOR_END();

    return CE_OK;
}

static enConsoleResult showtime(int argc, char** argv)
{
    KPrint("\r\n");
    while(1){
        if( is_key_press() == 'q' ){
            break;
        }
        int year, month, day, hour, minute, second;
        if( RtcGetDatetime(&year, &month, &day, &hour, &minute, &second) == OK){
            KPrintf("\r%d/%02d/%02d %02d:%02d:%02d", (uint32_t)year, (uint32_t)month, 
                    (uint32_t)day, (uint32_t)hour, (uint32_t)minute, (uint32_t)second);
            atomTimerDelay(100);
        }else{
            SYSLOG(LOG_WARNING, "no rtc module found!");
            break;
        }
    }
    return CE_OK;
}

#ifdef ATOM_TSTATUS_CHECKING

static enConsoleResult print_thread_status(int argc, char** argv)
{
    atomThreadStatusPrint();
	return CE_OK;
}

#endif

BGEIN_DEF_CMD(_shellCmds)
    CMD_DEF(version, "show version")
    CMD_DEF(showtime, "low power mode rtc")
    CMD_DEF_ALISA(ps, print_thread_status, "show task status")
END_DEF_CMD()

#endif  /* COM_USE_CONSOLE && COM_USE_SHELL_CMD*/

void ShellInit(void)
{
#ifdef COM_USE_SHELL_CMD
	CONSOLE_REG_CMD(_shellCmds);
#endif    
}

